<template>
  <div class="w">

    <!-- 标题 -->
    <div class="title-area">
      <span>
        产品信息
      </span>
    </div>

    <!-- 主体内容显示区 -->
    <div class="content-area">

      <!-- 查询表单区 -->
      <div class="form-area">
        <Form
          name="form"
          ref="formRef"
          :labelCol="{span: 5, offset: 0}"
          @finish="formFinish"
          :initialValues="queryForm">

          <Row>
            <Col :span="8">
              <FormItem name="produceName" label="产品名称" initialValue="">
                <Input placeholder="请输入产品名称" style="width: 70%" />
              </FormItem>
            </Col>
            <Col :span="8">
              <FormItem name="release" label="版本号" initialValue="">
                <Input placeholder="请输入版本号" style="width: 70%" />
              </FormItem>
            </Col>
            <Col :span="8">
              <FormItem :wrapperCol="{span: 20, offset: 5}">
                <Button type="primary" @click="formQuery">
                  <Icon name="search" style="margin-top: 3px; margin-right: 5px;" /> 查 询
                </Button>
                <Button style="margin-left: 10px" @click="formReset">
                  <Icon name="rollback-outline" style="margin-top: 3px; margin-right: 5px;" /> 重 置
                </Button>
              </FormItem>
            </Col>
          </Row>
        </Form>
      </div>

      <!-- 分割线 -->
      <Divider />

      <!-- 按钮区 -->
      <div class="button-area">
        <Button type="primary" @click="tblOptForm()">
          <Icon name="add" /> 新增
        </Button>
      </div>

      <!-- 列表区 -->
      <Skeleton
        style="margin-top: 15px;"
        :loading="isDataLoading"
        :avatar="false"
        :paragraph="{ rows: 15 }"
        active>

        <Table
          style="margin-top: 20px"
          :columns="tableColumns"
          :dataSource="tableData"
          :bordered="false"
          :rowKey="(record) => record.key"
          :scroll="{ y: 470 }"
          @change="handleTableChange"
          :pagination="{
            total: total,
            showQuickJumper: true,
            showSizeChanger: true,
            pageSize: queryForm.pageSize,
            current: queryForm.pageNum,
          }">
          <template #render="render">
            <div v-if="render.dataIndex == 'firmName'">
              <span>{{render.record.firm.firmName}}</span>
            </div>
            <div v-else-if="render.dataIndex == 'remark'">
              <span>
                {{ (render.record.remark && render.record.remark.length) > 50 ? render.record.remark.substr(0, 50) + '...' : render.record.remark }}
              </span>
            </div>
            <div v-else-if="render.dataIndex == 'platform'">
              <span>
                <dict-tag :options="dict.type.platform" :value="render.record.platform" />
              </span>
            </div>
            <div v-else-if="render.dataIndex == 'operation'">
              <a href="#" @click.prevent="tblOptForm(render.record)">
                <Icon name="edit" style="margin-top: 4px;" /> 编辑
              </a>
              <a href="#" @click.prevent="removeProduce(render.record)">
                <Icon name="delete-can" style="margin-top: 4px;" /> 删除
              </a>
            </div>
          </template>
          <template #paginationTotal="props">
            <span>总共 {{ props.total }} 条</span>
          </template>
        </Table>
      </Skeleton>
    </div>

    <!-- 表单页对话框 -->
    <Modal
      class="fromModal"
      :title="formDialogInfo.title"
      v-model="formDialogInfo.visible"
      @cancel="formDialogInfo.visible = false"
      width="1100px"
      :bodyStyle="{ padding: '0px' }"
      :footer="[null]">

      <produce-form 
        :key="Date.now()" 
        :produceId="formDialogInfo.produceId" 
        :currRow="formDialogInfo.currRow" 
        :refData="{
          platforms: dict.type.platform,
          firmsList: firmsList,
          produceDetail: produceDetail
        }" 
        @handleCancel="formDialogCancel" />
    </Modal>
  </div>
</template>

<script>
import { Message } from '@uni/vue';
import { listProduce, getProduce, delProduce } from "@/api/doapp/produce";
import produceForm from "./form.vue"

export default {
  name: "app",
  dicts: ['platform'],
  components: {
    produceForm
  },
  data: function () {
    return {
      firmsList: [],
      formDialogInfo: {
        title: '',
        visible: false,
        produceId: 0,
        currRow: {}
      },
      produceDetail: {},
      isDataLoading: false,
      queryForm: {
        produceName: '',
        release: ''
      },
      tableColumns: [
        // {"title":"序号", "dataIndex":"produceId", "key":"produceId", "align":"center", width:50},
        {"title":"产品名称", "dataIndex":"produceName", "key":"produceName", "align":"center", width:200},
        {"title":"厂商名称", "dataIndex":"firmName", "key":"firmName", "align":"center", width:250},
        {"title":"版本号", "dataIndex":"release", "key":"release", "align":"center", width:100},
        {"title":"产品描述", "dataIndex":"remark", "key":"remark", "align":"center"},
        {"title":"适用平台", "dataIndex":"platform", "key":"platform", "align":"center"},
        {"title":"操作", "dataIndex":"operation", "key":"operation", "align":"center", "width":120}
      ],
      tableData: [],
      total: 0
    }
  },
  mounted: function () {
    this.formFinish();
  },
  methods: {
    queryProduceDetail: async function (id) {
      const rs = await getProduce(id);
      if (rs.code !== 200) return Message.error({ text: '获取产品明细失败：' + rs.msg });
      this.produceDetail = rs.data ? rs.data : {};
      this.firmsList = rs.firms;
    },
    formQuery: function () {
      this.$refs.formRef.submit();
    },
    formReset: function () {
      this.$refs.formRef.reset();
    },
    queryList: async function () {
      this.isDataLoading = true;
      const rs = await listProduce(this.queryForm);
      if (rs.code !== 200) return Message.error({ text: '获取产品列表信息失败：' + rs.msg });
      this.tableData = rs.rows;
      this.total = rs.total;
      // console.log("rs: ", rs);
      this.isDataLoading = false;
    },
    formFinish: async function (formValues) {
      this.queryForm = {
        pageNum: 1,
        pageSize: 10,
        produceName: formValues ? formValues.produceName : '',
        release: formValues ? formValues.release : ''
      };
      this.queryList();
    },
    handleTableChange: function (p, filters) {
      const { current, pageSize } = p;
      this.queryForm.pageNum = current;
      this.queryForm.pageSize = pageSize;
      this.queryList();
      console.log('filters', p, filters);
    },
    tblOptForm: async function (record) {
      this.formDialogInfo.produceId = record ? record.produceId : 0;
      this.formDialogInfo.currRow = record ? record : {};
      this.formDialogInfo.title = (record ? '修改' : '新增') + '产品信息';

      await this.queryProduceDetail(record ? record.produceId : null);
      this.formDialogInfo.visible = true;
    },
    formDialogCancel: function () {
      this.formDialogInfo.visible = false;
      this.formFinish();
    },
    removeProduce: function (record) {
      this.$MessageBox.ModalConfirm(`是否确认删除名称为 "${record.produceName}" 的数据项？`, async () => {
        const rs = await delProduce(record.produceId);
        if (rs.code !== 200) return Message.error({ text: '删除产品失败：' + rs.msg });
        this.formQuery();
        
        Message.success({ text: "删除产品信息完成" });
      });
    }
  }
}
</script>

<style scoped>
  .w {
    /* height: 100%; */
    background-color: #F9FAFB;
  }

  /* 标题 */
  .title-area {
    height: 36px;
    background-color: #E9EBF0;
  }
  .title-area span {
    display: inline-block;
    font-weight: 500;
    line-height: 36px;
    margin-left: 15px;
  }

  /* 主体内容显示区 */
  .content-area {
    /* background-color: lightblue; */
    height: 100%;
    margin-left: 25px;
    margin-right: 25px;
    padding-bottom: 15px;
  }

  /* 查询表单区 */
  .form-area {
    /* background-color: lightcyan; */
    margin-top: 15px;
    margin-left: 50px;
    margin-right: 50px;
  }

  .uni-divider {
    margin: 0;
  }

  /* 按钮区 */
  .button-area {
    margin-top: 15px;
    /* background-color: lightcyan; */
  }
  .button-area svg {
    margin-top: 3px;
    margin-right: 5px;
  }

  /* 列表区 */

  /* 弹框区 */
  ::v-deep .fromModal > .uni-modal-wrap > .uni-modal-wrap-content > .uni-modal-footer {
    padding: 0;
  }
</style>

